<?php
/**
 *	KLF Framework
 *
 *	Written by Kristian Oye <kristianoye@gmail.com> on Oct 23, 2011
 *	Copyright (C) 2010-2011
 */
namespace KLF\System\Web
{
	use KLF\KCore;
	use KLF\System\KDateTime;
	use KLF\System\Errors\KException;
	
	/**
	 *  Class KHttpCookie extends \KLF\System\KObject
	 *
	 *  Represents a single HTTP Cookie
	 *
	 *  @package KLF\System\Web
	 *  @access public
	 *  @author Kristian Oye <kristianoye@gmail.com>
	 *  @since 0.1
	 *  
	 *  @method KHttpCookie NewInstance
	 *  
	 *  @property string Domain
	 *  @property KDateTime Expires
	 *  @property string Name
	 *  @property string Path
	 *  
	 */
	class KHttpCookie extends \KLF\System\KObject
	{
		/**
		 * The domain which can access this cookie.
		 * 
		 * @var string
		 */
		protected $_propDomain;
		
		/**
		 * The date and time when the cookie expires.
		 * 
		 * @var KDateTime
		 */
		protected $_propExpires;
		
		/**
		 * The cookie's name.
		 * 
		 * @var string
		 */
		protected $_propName;
		
		/**
		 * The path restriction placed on the cookie.
		 * 
		 * @var string
		 */
		protected $_propPath;
		
		/**
		 * True if the cookie only applies to SSL connections.
		 * 
		 * @var bool
		 */
		protected $_propSecure;
		
		/**
		 * The value contained within the cookie.
		 * 
		 * @var string
		 */
		protected $_propValue;
		
		/**
		 * A collection of values 
		 * 
		 * @var array
		 */
		protected $_propMultivalue;
		
		/**
		 * Creates a new cookie.
		 * 
		 * @param string $cookieName
		 * @param mixed $value
		 * @param int $expire
		 * @param string $path
		 * @param bool $secure
		 * @param bool $httpOnly
		 */
		public function __construct($cookieName = '', $value = '', $expire = 0, $path = '/', $secure = false, $httpOnly = false)
		{
			$this->_propName = $cookieName;
			$this->_propValue = $value;
			$this->_propExpires = new KDateTime($expire);
		}
		
		/**
		 * Prints the KHttpCookie as a string.
		 * 
		 * (non-PHPdoc)
		 * @see KLF\System.KObject::__toString()
		 */
		public function __toString()
		{
			return KCore::FS("Cookie: {name}={value};path={path};domain={domain};expires={expires}", array(
				'name'		=> $this->_propName,
				'path'		=> $this->_propPath,
				'value'		=> $this->_propValue,
				'domain'	=> $this->_propDomain,
				'expires'	=> $this->_propExpires));
		}
		
		/**
		 * Setter for the Expires property.
		 * 
		 * @param string|KDateTime|int $arg
		 * @throws KException
		 */
		protected function propSetExpires($arg)
		{
			try
			{
				$dt = new KDateTime($arg);
				$this->_propExpires = $dt;
			}
			catch(KException $ex)
			{
				throw $ex;
			}
			return $this->_propExpires;
		}
	}
}
